package com.wdh.kjs.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.PlatformInfoForAppHtml;
import com.wdh.kjs.model.PlatformInfoForAppModel;

/**
 * Created by feifei on 2016/8/30.
 */
public class JieKuanXYActivity extends BaseTopActivity {
    private WebView wv_jiekuan;
    private PlatformInfoForAppHtml pifah;
    private String sign;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jiekuanlayout);
        wv_jiekuan = (WebView) findViewById(R.id.wv_jiekuan);
        sign = getIntent().getStringExtra("sign");
        if (sign.equals("3"))
        initTopBar("借款协议");
        if (sign.equals("4"))
            initTopBar("入仓托管协议");
        if (sign.equals("5"))
            initTopBar("动产质押合同");
        if (sign.equals("6"))
            initTopBar("入仓托管转让协议");
        if (sign.equals("7"))
            initTopBar("借款协议-金融贷");
        if (sign.equals("9"))
            initTopBar("风险提示");
        initweb();
    }

    private void initweb() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("sign", sign);
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, Httpurl.URL_Platform_Inf_For_App, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                PlatformInfoForAppModel pifam = gson.fromJson(responseInfo.result, PlatformInfoForAppModel.class);
                if(pifam.isSuccess()) {
                    if (pifam.getResult().size() != 0) {
                        pifah = pifam.getResult().get(0);
                        PlatformInfoForAppHtml.htmlStr = pifah.getContent();
                        wv_jiekuan.loadDataWithBaseURL(null, PlatformInfoForAppHtml.htmlStr, "text/html", "utf-8", null);
                        wv_jiekuan.getSettings().setJavaScriptEnabled(true);
                        wv_jiekuan.setWebChromeClient(new WebChromeClient());
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                falseDialogShow("网络异常");
            }
        });
    }


    private void falseDialogShow(String hint) {
        View dialog = (LinearLayout) getLayoutInflater().inflate(R.layout.view_dialog_false, null);
        Button bt_dialog_false_negative = (Button) dialog.findViewById(R.id.bt_dialog_false_negative);
        Button bt_dialog_false_positive = (Button) dialog.findViewById(R.id.bt_dialog_false_positive);
        TextView tv_dialog_false_hint = (TextView) dialog.findViewById(R.id.tv_dialog_false_hint);
        tv_dialog_false_hint.setText(hint);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialog);
        builder.create();
        final AlertDialog dialogShow = builder.show();
        bt_dialog_false_negative.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
            }
        });
        bt_dialog_false_positive.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialogShow.dismiss();
//                updateLoginPassword_inputmsg.setText("");
            }
        });
    }
}
